﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Speech.Synthesis;
using System.Speech.Recognition;


namespace MustangMath
{
    public partial class Form1 : Form
    {
        private SpeechSynthesizer ss = new SpeechSynthesizer();
        private SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
        private int answer, first, second, prod; 

        public Form1()
        {
            InitializeComponent();

            recognizer.SetInputToDefaultAudioDevice();
            Grammar customGrammar = CreateCustomGrammar();
            recognizer.UnloadAllGrammars();
            recognizer.LoadGrammar(customGrammar);
            recognizer.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            //recognizer.SpeechHypothesized += 
            //  new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
            recognizer.RecognizeAsync(RecognizeMode.Multiple);

            Next(); // move to the first problem

            progressBar1.Minimum = 0;
            progressBar1.Maximum = 10;
            progressBar1.Step = 1;
            progressBar1.Value = 0;
            progressBar1.Update();

        }

        private void recognizer_SpeechRecognized(object sender,
                        SpeechRecognizedEventArgs e)
        {
           answer = Int32.Parse(e.Result.Text);
           if (answer == prod)
           {
               label4.ForeColor = System.Drawing.Color.Green;
           }
           else
           {
               label4.ForeColor = System.Drawing.Color.OrangeRed;
           }

           label4.Text = e.Result.Text;
           label4.Visible = true;
           label4.Refresh();
           
        }

        //private void recognizer_SpeechHypothesized(object sender,
        //                SpeechHypothesizedEventArgs e)
        //{
        //    //label5.Text = "hypo:  " + e.Result.Text;
        //}

        private Grammar CreateCustomGrammar()
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            string[] sa = new string[101];
            for (int i=0;i<=100;i++)
            {
                sa[i] = i.ToString();
            }            
            grammarBuilder.Append(new Choices(sa));
            return new Grammar(grammarBuilder);
        }

        private void Next()
        {
            Random rc = new Random();
            first = rc.Next(0, 10);
            second = rc.Next(0,10);
            prod = first * second;
            answer = -1;

            label1.Text = first.ToString();
            label3.Text = second.ToString();
            label4.Text = prod.ToString();
            label4.Visible = false;
            this.Update();

            ss.Speak(label1.Text + " times " + label3.Text + " = ");

            timer1.Enabled = true;
            progressBar1.Value = 0;
            progressBar1.Update();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // see if correct answer was supplied yet
            if (answer==prod)
            {
                label4.ForeColor = System.Drawing.Color.Green;
                label4.Refresh();
                ss.Speak("correct");
                while (progressBar1.Value < progressBar1.Maximum)
                {
                    progressBar1.PerformStep();
                }
            }

            if (progressBar1.Value >= progressBar1.Maximum)
            {
                if (answer != prod)
                {
                    label4.Text = prod.ToString();
                    label4.ForeColor = System.Drawing.Color.Black;
                }
                label4.Visible = true;
                label4.Refresh();
                timer1.Enabled = false;
                progressBar1.Value = 0;

                if (answer != prod)
                {
                    //reinforce correct answer - say it, and leave it up for 5 seconds
                    ss.Speak(String.Format("{0} times {1} equals {2}", first.ToString(), second.ToString(), prod.ToString()));
                    // wait one second if it was wrong, to help them see the answer
                    System.Threading.Thread.Sleep(5000); 
                } 
                Next();
            }
            else
            {
                progressBar1.PerformStep();
            }
        }
    }
}
